package org.jeecg.modules.demo.annualkeywork.service.impl;

import org.jeecg.modules.demo.annualkeywork.entity.*;
import org.jeecg.modules.demo.annualkeywork.mapper.KeyWorkDepartmentsMapper;
import org.jeecg.modules.demo.annualkeywork.mapper.AnnualKeyWorkMapper;
import org.jeecg.modules.demo.annualkeywork.service.IAnnualKeyWorkService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 年度重点工作
 * @Author: jeecg-boot
 * @Date:   2025-08-19
 * @Version: V1.0
 */
@Service
public class AnnualKeyWorkServiceImpl extends ServiceImpl<AnnualKeyWorkMapper, AnnualKeyWork> implements IAnnualKeyWorkService {

	@Autowired
	private AnnualKeyWorkMapper annualKeyWorkMapper;
	@Autowired
	private KeyWorkDepartmentsMapper keyWorkDepartmentsMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(AnnualKeyWork annualKeyWork, List<KeyWorkDepartments> keyWorkDepartmentsList) {
		annualKeyWorkMapper.insert(annualKeyWork);
		if(keyWorkDepartmentsList!=null && keyWorkDepartmentsList.size()>0) {
			for(KeyWorkDepartments entity:keyWorkDepartmentsList) {
				//外键设置
				entity.setMainId(annualKeyWork.getId());
				keyWorkDepartmentsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(AnnualKeyWork annualKeyWork,List<KeyWorkDepartments> keyWorkDepartmentsList) {
		annualKeyWorkMapper.updateById(annualKeyWork);
		
		//1.先删除子表数据
		keyWorkDepartmentsMapper.deleteByMainId(annualKeyWork.getId());
		
		//2.子表数据重新插入
		if(keyWorkDepartmentsList!=null && keyWorkDepartmentsList.size()>0) {
			for(KeyWorkDepartments entity:keyWorkDepartmentsList) {
				//外键设置
				entity.setMainId(annualKeyWork.getId());
				keyWorkDepartmentsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		keyWorkDepartmentsMapper.deleteByMainId(id);
		annualKeyWorkMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			keyWorkDepartmentsMapper.deleteByMainId(id.toString());
			annualKeyWorkMapper.deleteById(id);
		}
	}
	//    查询年度重点工作列表
	@Override
	public List<AnnualKeyWorkList> queryKeyWorkList() {
		return annualKeyWorkMapper.queryKeyWorkList();
	}
	//    查询汇报年度重点工作列表
	@Override
	public List<ReportAnnualKeyWork> SelectKeyWorkList(String orgId) {
		return annualKeyWorkMapper.SelectKeyWorkList(orgId);
	}
	//    保存汇报年度重点工作，实现汇报功能
	@Override
	public int saveReportAnnualKeyWork(ReportAnnualKeyWork reportAnnualKeyWork) {
		return annualKeyWorkMapper.saveReportAnnualKeyWork(reportAnnualKeyWork);
	}
	//    查询年度重点工作的所有数据
	@Override
	public List<AnnualKeyWork> getAllList() {
		return annualKeyWorkMapper.getAllList();
	}
	//    查询责任部门的上级部门
	@Override
	public String SelectApprovalKeyWorkListByRespondepartment() {
		return annualKeyWorkMapper.SelectApprovalKeyWorkListByRespondepartment();
	}
	//    查询审批年度重点工作列表
	@Override
	public List<ApprovalAnnualKeyWork> SelectApprovalKeyWorkList() {
		return annualKeyWorkMapper.SelectApprovalKeyWorkList();
	}
	//    保存审批年度重点工作，实现审批功能
	@Override
	public int saveApprovalKeyWork(ApprovalAnnualKeyWork approvalKeyWork) {
		return annualKeyWorkMapper.saveApprovalKeyWork(approvalKeyWork);
	}

}
